package com.start.handler;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class MyExceptionController {

	@RequestMapping("/testMyException")
	public String testMyException() throws Exception{
		try {
			int i = 11/0;
		}catch (Exception e) {
			e.printStackTrace();
			throw new Exception("不能运算。。。");
		}
		return "successException";
	}
	/**
	 * 
	 * @return
	 * @throws MyException
	 */
	@RequestMapping("/testExceptionHandlerController")
	public String testExceptionHandlerController() throws MyException{
		try {
			int i = 11/0;
		}catch (Exception e) {
			e.printStackTrace();
			throw new MyException("不能运算。。。");
		}
		return "successException";
	}
	
	@ExceptionHandler
	public ModelAndView testExceptionHandler(MyException exception) {
		System.out.println("____-----------------...>>>>>====-----");
		System.out.println(exception.getMessage());
		ModelAndView view = new ModelAndView();
		view.addObject("abc", "159487");
		view.setViewName("successException");

		return view;
	}
	
	/**
	 * ResponseStatus 
	 * 
	 */
	@RequestMapping("/testResponseStatus")
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "500错误@！！！！")
	public String testResponseStatus () throws MyException{
		int i = 11/11;
		return "successException";
	}
	
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "500错误@！！！！")
	public ModelAndView testResponseStatus2() {
		System.out.println("500错误@！！！！--------");
		ModelAndView view = new ModelAndView();
		view.addObject("abc", "159487");
		view.setViewName("successException");

		return view;
	}
}
